package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Auther: 宋H
 * @Date: 2022/9/3
 * @Description: com.itheima.controller
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@Api(tags="员工信息相关接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return {@link com.itheima.controller.R<com.itheima.entity.Employee>}
     */

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //将页面提交的密码进行加密处理
        String pwd = employee.getPassword();
        pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());

        LambdaQueryWrapper<Employee> lq = new LambdaQueryWrapper<>();
        lq.eq(Employee::getUsername, employee.getUsername());

        Employee emp = employeeService.getOne(lq);

        if (Objects.isNull(emp)) {
            return R.error("用户名不存在");
        } else {
            if (!pwd.equals(emp.getPassword())) {
                return R.error("密码错误，请重新输入");
            } else if (emp.getStatus() == 0) {
                return R.error("用户已被禁用");
            }
            request.getSession().setAttribute("employee", emp.getId());//用户登录成功,把用户id存入session中
            return R.success(emp);
        }
    }

    /**
     * 退出登录
     *
     * @param request
     * @return {@link com.itheima.controller.R<java.lang.String>}
     */

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().getAttribute("employee");

        return R.success("退出成功");
    }

    /**
     * 新增员工信息
     *
     * @param request
     * @param employee
     * @return {@link com.itheima.controller.R<java.lang.String>}
     */


    @PostMapping
    public R<String> employeeSave(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工{}", employee.toString());
        //获取session中的用户id
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        boolean save = employeeService.save(employee);
        return R.success("添加成功");
    }


    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    //page是mp提供的对象，里面封装了需要的数据变量
    public R<Page> employeePage(int page, int pageSize, String name) {
        log.info("分页查询");
        Page pageInfo = new Page(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Employee> lq = new LambdaQueryWrapper();
        //进行条件过滤
        lq.like(Strings.isNotEmpty(name), Employee::getName, name);
        lq.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pageInfo, lq);
        // 将分页查询后的数据存入到R对象的data数据域中
        return R.success(pageInfo);
    }

    /**
     * 根据员工id修改员工信息
     *
     * @param request
     * @param employee
     */
    @PutMapping
    public R<String> employeeUpdata(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("根据员工id修改员工信息");
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * 根据用户iD查询到用户的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> employeeById(@PathVariable Long id) {
        log.info("查询员工id");
        Employee emp = employeeService.getById(id);
        if(emp!=null){
            return R.success(emp);
        }
        return R.error("未查询到相应的用户信息");
    }
}
